﻿using AutoMapper;
using BasicData.Domain;
using BasicData.ErrorCode;
using BasicData.Infastruucture;
using BasicData.Write.API.application.Command.Role;
using MediatR;

namespace BasicData.Write.API.application.CommandHandler.Role
{
    public class AddRoleCreateCommandHandler : IRequestHandler<AddRoleCreateCommand, ApiResult<object>>
    {

        private readonly IBaseRepository<RoleModel> roleRepo;
        private readonly IMapper mapper;

        public AddRoleCreateCommandHandler(IBaseRepository<RoleModel> roleRepo, IMapper mapper)
        {
            this.roleRepo = roleRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<object>> Handle(AddRoleCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> res = new ApiResult<object>();
            var role = mapper.Map<RoleModel>(request);
            int r = roleRepo.Create(role);
            if (r > 0)
            {
                res.Code = BaseEnums.Success;
                res.Msg = "角色添加成功";
                res.Data = r;
            }
            else
            {
                res.Code = BaseEnums.Failed;
                res.Msg = "角色添加失败";
                res.Data = r;
            }
            return Task.FromResult(res);
        }
    }
}
